package org.domeos.configuration;

import javax.servlet.DispatcherType;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.domeos.framework.api.service.auth.UserService;
import org.domeos.framework.shiro.filter.LocalSpyFilter;
import org.domeos.framework.shiro.filter.UserCreateFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class LoginConfg {
	@Autowired
	SecurityManager securityManager;
	@Autowired
    UserService userService;
	@Value("${gdocker.idcenter.url}")
	private String idcenterUrl;
	
	@Value("${gdocker.idcenter.suffix}")
	private String idcenterSuffix;
	/**
	 * FilterRegistrationBean
	 *
	 * @return
	 */
	@Bean(name = "localSpyFilter")
	public FilterRegistrationBean localSpyFilter() {
		SecurityUtils.setSecurityManager(securityManager);
		FilterRegistrationBean registration = new FilterRegistrationBean();
		registration.addInitParameter("systemName", "Gdocker");
		registration.addInitParameter("centerUrl", idcenterUrl);
		registration.addInitParameter("dnsSuffix", idcenterSuffix);
		registration.addInitParameter("aesPassword", idcenterUrl);
		registration.addInitParameter("cookieName", "IDCENTER");
		registration.addInitParameter("gotoUrl", idcenterUrl);
		LocalSpyFilter fil=new LocalSpyFilter();
		registration.setFilter(fil);
		registration.setEnabled(true);
		registration.addUrlPatterns("/loginIn/index");
		registration.setName("localSpyFilter");
		registration.setDispatcherTypes(DispatcherType.REQUEST);
		registration.setOrder(0);
		return registration;
	}
	
	@Bean(name = "userCreateFilter")
	public FilterRegistrationBean userCreateFilter() {
		FilterRegistrationBean registration = new FilterRegistrationBean();
		UserCreateFilter fil=new UserCreateFilter();
		fil.setUserService(userService);
		registration.setFilter(fil);
		registration.setEnabled(true);
		registration.addUrlPatterns("/loginIn/index");
		registration.setName("userCreateFilter");
		registration.setDispatcherTypes(DispatcherType.REQUEST);
		registration.setOrder(1);
		return registration;
	}
}
